using Coding4Fun.Phone.Controls.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Coding4Fun.Phone.Controls
{
	public class ColorHexagonPicker : ColorBaseControl
	{
		private Rectangle rectangle_0;
		private bool bool_0;
		private bool bool_1;
		private readonly List<Rectangle> list_0 = new List<Rectangle>();
		public static readonly DependencyProperty SelectedStrokeColorProperty = DependencyProperty.Register("SelectedStrokeColor", typeof(Color), typeof(ColorHexagonPicker), new PropertyMetadata(Color.FromArgb(255, 255, 255, 255)));
		public static readonly DependencyProperty ColorDarknessStepsProperty = DependencyProperty.Register("ColorDarknessSteps", typeof(int), typeof(ColorHexagonPicker), new PropertyMetadata(2, new PropertyChangedCallback(ColorHexagonPicker.smethod_3)));
		public static readonly DependencyProperty ColorBrightnessStepsProperty = DependencyProperty.Register("ColorBrightnessSteps", typeof(int), typeof(ColorHexagonPicker), new PropertyMetadata(4, new PropertyChangedCallback(ColorHexagonPicker.smethod_3)));
		public static readonly DependencyProperty GreyScaleStepsProperty = DependencyProperty.Register("GreyScaleSteps", typeof(int), typeof(ColorHexagonPicker), new PropertyMetadata(20, new PropertyChangedCallback(ColorHexagonPicker.smethod_3)));
		public static readonly DependencyProperty ColorSizeProperty = DependencyProperty.Register("ColorSize", typeof(double), typeof(ColorHexagonPicker), new PropertyMetadata(24.0, new PropertyChangedCallback(ColorHexagonPicker.smethod_3)));
		public static readonly DependencyProperty ColorBodyProperty = DependencyProperty.Register("ColorBody", typeof(object), typeof(ColorHexagonPicker), new PropertyMetadata(null));
		public static readonly DependencyProperty GreyScaleBodyProperty = DependencyProperty.Register("GreyScaleBody", typeof(object), typeof(ColorHexagonPicker), new PropertyMetadata(null));
		public Color SelectedStrokeColor
		{
			get
			{
				return (Color)base.GetValue(ColorHexagonPicker.SelectedStrokeColorProperty);
			}
			set
			{
				base.SetValue(ColorHexagonPicker.SelectedStrokeColorProperty, value);
			}
		}
		public int ColorDarknessSteps
		{
			get
			{
				return (int)base.GetValue(ColorHexagonPicker.ColorDarknessStepsProperty);
			}
			set
			{
				base.SetValue(ColorHexagonPicker.ColorDarknessStepsProperty, value);
			}
		}
		public int ColorBrightnessSteps
		{
			get
			{
				return (int)base.GetValue(ColorHexagonPicker.ColorBrightnessStepsProperty);
			}
			set
			{
				base.SetValue(ColorHexagonPicker.ColorBrightnessStepsProperty, value);
			}
		}
		public int GreyScaleSteps
		{
			get
			{
				return (int)base.GetValue(ColorHexagonPicker.GreyScaleStepsProperty);
			}
			set
			{
				base.SetValue(ColorHexagonPicker.GreyScaleStepsProperty, value);
			}
		}
		public double ColorSize
		{
			get
			{
				return (double)base.GetValue(ColorHexagonPicker.ColorSizeProperty);
			}
			set
			{
				base.SetValue(ColorHexagonPicker.ColorSizeProperty, value);
			}
		}
		public object ColorBody
		{
			get
			{
				return base.GetValue(ColorHexagonPicker.ColorBodyProperty);
			}
			set
			{
				base.SetValue(ColorHexagonPicker.ColorBodyProperty, value);
			}
		}
		public object GreyScaleBody
		{
			get
			{
				return base.GetValue(ColorHexagonPicker.GreyScaleBodyProperty);
			}
			set
			{
				base.SetValue(ColorHexagonPicker.GreyScaleBodyProperty, value);
			}
		}
		public ColorHexagonPicker()
		{
			base.set_DefaultStyleKey(typeof(ColorHexagonPicker));
			base.add_Loaded(new RoutedEventHandler(this.ColorHexagonPicker_Loaded));
		}
		private void ColorHexagonPicker_Loaded(object sender, RoutedEventArgs e)
		{
			this.bool_0 = true;
			this.GenerateLayout();
		}
		protected internal override void UpdateLayoutBasedOnColor()
		{
			if (this.bool_1)
			{
				return;
			}
			base.UpdateLayoutBasedOnColor();
			this.method_5(Enumerable.SingleOrDefault<Rectangle>(Enumerable.Where<Rectangle>(this.list_0, new Func<Rectangle, bool>(this.method_6))));
		}
		public void GenerateLayout()
		{
			if (!this.bool_0)
			{
				return;
			}
			int num = this.ColorBrightnessSteps + this.ColorDarknessSteps;
			this.GreyScaleBody = null;
			this.ColorBody = null;
			this.list_0.Clear();
			if (num > 0)
			{
				StackPanel stackPanel = new StackPanel();
				for (int i = 0; i < num; i++)
				{
					int int_ = num + i + 1;
					stackPanel.get_Children().Add(this.method_2(int_, this.method_0(num, i)));
				}
				stackPanel.get_Children().Add(this.method_2(num * 2 + 1, 0.0));
				for (int j = num - 1; j >= 0; j--)
				{
					int int_2 = num + j + 1;
					stackPanel.get_Children().Add(this.method_2(int_2, -this.method_0(num, j)));
				}
				this.ColorBody = stackPanel;
			}
			if (this.GreyScaleSteps > 0)
			{
				StackPanel stackPanel2 = new StackPanel();
				stackPanel2.set_Margin(new Thickness(0.0, this.ColorSize, 0.0, 0.0));
				StackPanel stackPanel3 = ColorHexagonPicker.smethod_2();
				StackPanel stackPanel4 = ColorHexagonPicker.smethod_2();
				int num2 = this.GreyScaleSteps + 2;
				double num3 = 255.0 / (double)num2;
				for (int k = 0; k <= num2; k++)
				{
					byte b = (byte)(num3 * (double)k);
					Rectangle rectangle = this.method_3(Color.FromArgb(255, b, b, b));
					if (k % 2 == 0)
					{
						stackPanel3.get_Children().Add(rectangle);
					}
					else
					{
						stackPanel4.get_Children().Add(rectangle);
					}
				}
				stackPanel2.get_Children().Add(stackPanel3);
				stackPanel2.get_Children().Add(stackPanel4);
				this.GreyScaleBody = stackPanel2;
			}
		}
		private double method_0(int int_0, int int_1)
		{
			return this.ColorSize * (double)(int_0 - int_1 - 1);
		}
		private static double smethod_0(double double_0, double double_1)
		{
			return Math.Sqrt(Math.Pow(double_0, 2.0) + Math.Pow(double_1, 2.0));
		}
		private static double smethod_1(double double_0, double double_1)
		{
			double num = Math.Atan2(double_1, double_0) * 57.295779513082323;
			if (num <= 0.0)
			{
				return num + 360.0;
			}
			return num;
		}
		private static StackPanel smethod_2()
		{
			StackPanel stackPanel = new StackPanel();
			stackPanel.set_HorizontalAlignment(1);
			stackPanel.set_Orientation(1);
			return stackPanel;
		}
		private float method_1(int int_0, double double_0)
		{
			return (float)(double_0 / ((double)int_0 * this.ColorSize));
		}
		private StackPanel method_2(int int_0, double double_0)
		{
			StackPanel stackPanel = ColorHexagonPicker.smethod_2();
			float num = (float)int_0 / 2f;
			for (int i = 1; i <= int_0; i++)
			{
				double double_ = ((double)((float)i - num) - 0.5) * this.ColorSize;
				double num2 = ColorHexagonPicker.smethod_0(double_, double_0);
				float float_ = (float)ColorHexagonPicker.smethod_1(double_, double_0);
				Color color_;
				if (this.ColorSize * (double)this.ColorBrightnessSteps >= num2)
				{
					color_ = ColorSpace.ConvertHsvToRgb(float_, this.method_1(this.ColorBrightnessSteps, num2), 1f);
				}
				else
				{
					double num3 = (double)(this.ColorBrightnessSteps + this.ColorDarknessSteps + 1) * this.ColorSize;
					num2 = num3 - num2;
					color_ = ColorSpace.ConvertHsvToRgb(float_, 1f, this.method_1(this.ColorDarknessSteps + 1, num2));
				}
				stackPanel.get_Children().Add(this.method_3(color_));
			}
			return stackPanel;
		}
		private Rectangle method_3(Color color_0)
		{
			Rectangle rectangle = new Rectangle();
			rectangle.set_Width(this.ColorSize);
			rectangle.set_Height(this.ColorSize);
			rectangle.set_StrokeThickness(3.0);
			rectangle.set_Stroke(new SolidColorBrush(color_0));
			rectangle.set_Fill(new SolidColorBrush(color_0));
			Rectangle rectangle2 = rectangle;
			rectangle2.add_MouseEnter(new MouseEventHandler(this.method_4));
			if (base.Color == color_0)
			{
				this.method_5(rectangle2);
			}
			this.list_0.Add(rectangle2);
			return rectangle2;
		}
		private void method_4(object sender, MouseEventArgs e)
		{
			Rectangle rectangle = sender as Rectangle;
			if (rectangle != null)
			{
				this.method_5(rectangle);
			}
		}
		private void method_5(Rectangle rectangle_1)
		{
			if (rectangle_1 == null)
			{
				return;
			}
			rectangle_1.set_Stroke(new SolidColorBrush(this.SelectedStrokeColor));
			if (this.rectangle_0 != null && this.rectangle_0 != rectangle_1)
			{
				this.rectangle_0.set_Stroke(this.rectangle_0.get_Fill());
			}
			this.rectangle_0 = rectangle_1;
			this.bool_1 = true;
			base.ColorChanging(((SolidColorBrush)rectangle_1.get_Fill()).get_Color());
			this.bool_1 = false;
		}
		private static void smethod_3(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			ColorHexagonPicker colorHexagonPicker = dependencyObject_0 as ColorHexagonPicker;
			if (colorHexagonPicker != null && dependencyPropertyChangedEventArgs_0.get_NewValue() != dependencyPropertyChangedEventArgs_0.get_OldValue())
			{
				colorHexagonPicker.GenerateLayout();
			}
		}
		[CompilerGenerated]
		private bool method_6(Rectangle rectangle_1)
		{
			return ((SolidColorBrush)rectangle_1.get_Fill()).get_Color() == base.Color;
		}
	}
}
